
$(function () {
    $("body").on("click","#saveRule",function () {
        addIntegralRule();
    }).on("click","#alertImgBtn",function () {
        alertImg();
    }).on("click","#inviteImgBtn",function () {
        inviteImg();
    }).on("change","#example1",function () {
        updateIntegralConfig("alertFrom","ALERT")
    }).on("change","#example2",function () {
        updateIntegralConfig("inviteForm","INVITE")
    }).on("click","#inviteBtn",function () {
        updateIntegralConfig("inviteForm","INVITE")
    })

})

//
/**
 * 积分规则修改弹窗
 * @param title 积分规则名称
 * @param ruleKey 规则Key
 * @param isList  规则数据类型是否为集合
 */
function openUpdateProtect(title,ruleKey,isList) {
    var type = $("#integralType").val();
    if (isList == undefined) {
        isList = false;
    }

    parent.layer.open({
        type: 2,
        area: ['690px','360px'],
        skin: 'layui-layer-demo',
        title: title || '积分规则设置',
        content: baseloaction + "/admin/user/cus/" + type + "/toUpdateIntegralRule?key=" + ruleKey + "&isList=" + isList,
        shade: [0.3, '#000000'],
    });
}
function addIntegralRule() {
    var params = $("#integralRuleForm").serializeJSON();
    params = JSON.stringify(params);
    $.ajax({
        url:baseloaction+"/admin/user/cus/addIntegralRule",
        data:params,
        type:"post",
        dataType: "json",
        contentType:"application/json",
        success:function (res) {
            if (res.success == true) {
                icModale("保存成功")
                window.location.reload();
            }
        }
    })
}



function alertImg() {
    initUpdateFileWin({
        fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
        callback:'alertImgCallback',
        cut:true,//开启剪切
        imgSize:DEFAULT_CLASS_IMG_SIZE,//剪切上传后生成图片的规格（开启剪切才起使用）
        aspectRatio:1/1//剪切区域的高宽比例（开启剪切才起使用）
    });
}

function inviteImg() {
    initUpdateFileWin({
        fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
        callback:'inviteImgCallback'
    });
}
function alertImgCallback(result) {

    if(result!=null){
        $("#alertImgSrc").val(result.pcUrlMap.small);
        $("#alertImg").attr("src",result.pcUrlMap.small);
        updateIntegralConfig("alertFrom","ALERT")
    }
}

function inviteImgCallback(result) {
    console.log(result)
    if(result!=null){
        $("#inviteImg").attr("src",result.url);
        $("#inviteImgSrc").val(result.url);
        updateIntegralConfig("inviteForm","INVITE")
    }
}


function updateIntegralConfig(id,key) {
    var params = $("#" + id).serializeJSON();
    if (params.switch == undefined) {
        params.switch='off'
    }
    var data={
        key:key,
        content: params
    };
    $.ajax({
        url:baseloaction+"/admin/user/cus/updateIntegralRuleConfig",
        data: JSON.stringify(data),
        dataType: "json",
        type:"post",
        contentType:"application/json",
        success:function (res) {
            parent.layer.closeAll()
            if (res.success==true) {
                icModale(0,"修改成功");
            }else{
                icModale(0, res.msg);
            }
            getRedirectMenu('/admin/user/cus/config/toIntegralRule',false)
        }
    })
}



